package com.gmail.bloim.serg.testrunner;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

/**
 * User: Администратор
 * Date: 25.04.13
 * Time: 11:27
 */
public class TestRunner {
    private File programRoot;
    private Properties testDefaults = new Properties();
    private static DateFormat dateFormat = new SimpleDateFormat();

    public TestRunner(String testRunnerDir, String programDir) {
        try {
            testDefaults.load(new FileInputStream(new File(testRunnerDir, "test.default.properties")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        programRoot = new File(programDir);
        try {
            dateFormat = new SimpleDateFormat(testDefaults.getProperty("out.dir.format", "dd_HH-mm-ss-SS"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String testRunnerDir = "TestRunner";
        String programDir = null;
        programDir = args[0];
        TestRunner testRunner = new TestRunner(testRunnerDir, programDir);
        testRunner.runTests();
    }

    private void runTests() {

        String testsDirStr = "test";
        File testsDir = new File(programRoot, testsDirStr);
        TestScanner scanner = new TestScanner(testsDir, testDefaults);
        Set<Test> tests = scanner.scanTests();
        String mainClass = scanner.getMainClass();
        File outDir = FileUtils.getFile(programRoot.getName(), "out", dateFormat.format(new Date()));
        outDir.mkdirs();
        TestExecuter executer = new TestExecuter(mainClass);
        executer.subscribeToTestRunOutput(new FileWriterTestSubscriber(outDir));
        TestResultHandlerObserver resultHandlerObserver = new TestResultHandlerObserver();
        resultHandlerObserver.add(new TestEnvironmentMaintainer());
        AccumulateTestResultHandler resultAccumulator = new AccumulateTestResultHandler();
        resultHandlerObserver.add(resultAccumulator);
//        TestTimeRegistrator timeRegistrator = new TestTimeRegistrator();
        for (Test test : tests) {
            try {
//                timeRegistrator.onTestStart(test);
                resultHandlerObserver.onTestStart(test);
                boolean res = executer.runTest(test);
//                timeRegistrator.onTestFinish(test);
                if (res)
                    resultHandlerObserver.onTestSuccess(test);
                else
                    resultHandlerObserver.onTestFail(test);
            } catch (TestEnvironmentException e) {
                e.printStackTrace();
            } catch (TestException e) {
                resultHandlerObserver.onTestException(test, e);
            }
        }
        System.out.println("Results");
        System.out.println();
        resultAccumulator.printTestStatistics(System.out, System.err);
    }
}
